import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.net.*;
import java.sql.Connection;
import java.sql.DataTruncation;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.codec.binary.Base64;

import com.atlassian.jira.rest.client.*;
import com.atlassian.jira.rest.client.domain.*;
import com.atlassian.jira.rest.client.domain.input.*;
import com.atlassian.jira.rest.client.internal.jersey.*;
import com.sun.jersey.api.client.UniformInterfaceException;


/*
@ Dr. Ashish
-----------------
@Sangeeta
@Change 1: Added parser for History.
@Change==>Date field in History. Done:)
@Check2: getDescription()  removes all the values in <>. Check can it also removes some needed "&#\\d\\d"
@Check3: clean_and_add function needs some more modification. It needs removal of spaces from the beginning.
@Check4: getDate2()  function is consistent with getDate1() as it adds .0 at the end of string.
@Check5: Just check how the "," in the component name works, as it might not be suitable to store it like this.
@
@Not supported:
 1. getLable
 2. AddFunctioality for ADD files in SVN example bugid 4004. DONE
 3. Run this program only for FIXED bug reports, NOT DONE Currently(Just to explore things). 
 --------------
New
*/

public class JBossIssueDownloader
{
private String jboss="https://issues.jboss.org/browse/";	
private String history_path="?page=com.atlassian.jira.plugin.system.issuetabpanels%3Achangehistory-tabpanel#issue-tabs";
private String subversion_path="?page=com.atlassian.jira.plugin.ext.subversion:subversion-commits-tabpanel#issue-tabs";
//private String issueid="ISPN-469";
private String file_path="";

public String userid="root";
public String passwd="123";
public String dbname="2jboss";

public ArrayList<String> bugids;

private static Connection conn;


	public void process(){
		
		try{
			
			
			JerseyJiraRestClientFactory factory = new JerseyJiraRestClientFactory();
	        URI jiraServerUri = new URI("https://issues.jboss.org");
	        JiraRestClient restClient = factory.createWithBasicHttpAuthentication(jiraServerUri, "sangeetalal", "iiitd123");
	        NullProgressMonitor pm = new NullProgressMonitor();
	        
	        bugids =  getBugids(conn);
	        
	        int i=bugids.indexOf("JBAS-8710");
	        
	        for(i=i;i<bugids.size();i++)
	        {
	        	try
	        	{
	        		String issueString = bugids.get(i);
	        		//System.out.println("IssueId="+issueString);
	        		Issue issue = restClient.getIssueClient().getIssue(issueString, pm);
	       	  
	        		System.out.println("--------------------------------------------------------------");
	        	
	        		String issueKey = issue.getKey();
	        		System.out.println("ISSUE KEY : " + issueKey); 
	        		
	        	
	        	/*	
	        		//java.sql.Timestamp date_reported =getDate2(issue.getCreationDate().toString());
	        	    //System.out.println("Date="+issue.getCreationDate().toString());
	        		
	        	
	        		String issueType = issue.getIssueType().getName();
	        		//System.out.println("ISSUE TYPE : " + issueType); 
	        	
	        		String issueStatus = issue.getStatus().getName();
	        		//System.out.println("ISSUE STATUS : " + issueStatus); 
	        	
	        		//BasicResolution tempResolution=issue.getResolution();
	        		//String issueResolution=tempResolution.getName();
	        		//issueResolution=issueResolution.replaceAll("\'", "");//Won't fIX RESOLUTION
	        		//System.out.println("Issue Resolution :"+issueResolution);
	        		String issueResolution="unresolved";
	        		try
	        		 {
	        		  BasicResolution tempResolution=issue.getResolution();
	        	 	  issueResolution=tempResolution.getName();
	        		  issueResolution=issueResolution.replaceAll("\'", "");//Won't fIX RESOLUTION
	        		 }catch(Exception e)
	        		 {System.out.println("Resolution exception");}
	        	   
	        		
	        		//java.sql.Timestamp issueResolvedDate=issue.get
	        		
	        		
	        		String issueSummary = issue.getSummary();
	        		issueSummary=issueSummary.replaceAll("\'", "");
	        		//System.out.println("ISSUE SUMMARY : " + issueSummary);
	        	      	
	        		
	        		//As get components is not directly supported
	        		Iterable<BasicComponent> tempComponents=issue.getComponents();
	        		Iterator comp_itr=tempComponents.iterator();
	        		String issueComponents="";
	        		while(comp_itr.hasNext())
	        			{
	        				String element=comp_itr.next().toString();
	        				String temp_str[]=element.split(","); 		
	        				String temp2_str[]=temp_str[1].split("=");
	        				//System.out.println("Issue com"+temp2_str[1]);
	        		
	        				issueComponents+=temp2_str[1]+",";
	        		
	        			}	
	        		System.out.println("IssueComponent: "+issueComponents);
	          
	          
	        		//assigness
	        		String issueAssigneeId="unassigned";
	        		String issueAssigneeName="unassigned";
	        		try
	        			{
	        			 issueAssigneeId= issue.getAssignee().getName();
	        			 issueAssigneeName=issue.getAssignee().getDisplayName();
	        			 issueAssigneeId= issueAssigneeId.replace("\'", " ");
	        			 issueAssigneeName=issueAssigneeName.replace("\'", " "); 
	        		     //System.out.println("Assignee id="+issueAssigneeId);
	        		     //System.out.println("Assignee Name="+issueAssigneeName);
	        			}catch(Exception e)
	        			{
	        				System.out.println("Assignee Exception");
	        			}
	          
	        		//Attachments
	        		Iterable<Attachment> tempAttachments=issue.getAttachments();
	        		Iterator attach_itr = tempAttachments.iterator();
	        		String issueAttachments="";
              
	        		while(attach_itr.hasNext())
	        			{
	        				issueAttachments+=attach_itr.next().toString()+"\n"; 
	        			}//wh
              
	        		System.out.println("IssueAttachments : "+issueAttachments);
	        		int hasAttachment=1;
	        		if(issueAttachments.equals(""))
	        		{
	        			hasAttachment=0;
	        		}
                  // System.out.println("hasAttachment: "+hasAttachment);
	        		
	        		// Reporter
	        		String issueReporter= issue.getReporter().getDisplayName();//taking display name
	        		issueReporter=issueReporter.replace("\'", " ");
	        		//System.out.println("Reporter: "+issueReporter);
	        		
	        		*/
	        		//
	        	
	        		
	        		String content=getContent(issueString,history_path);
	        		//System.out.println("conetnt="+content);
	        	/*
	            	String Description =getDescription(content);
	            	//System.out.println("Description="+Description);
	        	
	            	
	        		//ArrayList<history_object> history=getHistory(content); ///@Not a comment History infact
	        	*/
	        		
	        		String svn_content=getContent(issueString,subversion_path);//changing content to retrieve the subversion details
	        		ArrayList<svn_object> svn_commit_info= getSvnInfo(svn_content);
	        		
	        		
	        		String issueSvn_ids=format_svn_commit_info(svn_commit_info,1);
	        		String issueSvn_logmess=format_svn_commit_info(svn_commit_info,2);
	        		String issueSvn_files=format_svn_commit_info(svn_commit_info,3);
	        		String issueSvn_auth=format_svn_commit_info(svn_commit_info,4);
	        		
	        		System.out.println("issuseSVNids: "+issueSvn_ids);
	        		System.out.println("issueSvn_logmess: "+issueSvn_logmess);
	        		System.out.println("issueSvn_files: "+issueSvn_files);
	        		System.out.println("issueSvn_auth: "+issueSvn_auth);
	        	   	
	        		
	        		
	        		/*insert_to_db(issueKey, issueType,issueSummary,Description,issueComponents,issueStatus,
	        					issueResolution,hasAttachment,issueReporter,issueAssigneeName,
	        					issueSvn_ids,issueSvn_logmess,issueSvn_files,issueSvn_auth);*/
	        		
	        		insert_svn_to_db(svn_commit_info);
	        		
	        		
	        		
	        		/*java.sql.Timestamp date_reported = getDateCreated(content); 
	        		System.out.println("Date="+date_reported);
	        		insert_date_to_table(issueKey,date_reported);
	        		
	        		/*Iterable<Version> version  = issue.getAffectedVersions();
	        		Iterator itr=version.iterator();
	        		
	        		while(itr.hasNext())
	        		{
	        			System.out.println("itr="+itr.next());
	        		}
	        		*/
	        		System.out.println("--------------------------------------------------------------");
	        		
	        	}catch(RestClientException e)
	        		{
	        			System.out.println("Rest client Exception");
	        			
	        		}
		       
	        	
		        
	        }//for
			 
			//String content=getContent2("ISPN-836");
        	//System.out.println("conetnt="+content);
        	
        	//String Description =getDescription(content);
        	
        	//ArrayList<history_object> history=getHistory(content); 
        	
        	//ArrayList<svn_object> svn_commit_info= getSvnInfo(content);
        	
        	
	               
		}
		catch(RestClientException e)
		 {
			e.printStackTrace();
		 	System.out.println("Rest Exception caught");
		 }
		catch(Exception e){
			System.out.println("Caught");
			e.printStackTrace();
		}
	}
	


	
public ArrayList<String> getBugids(Connection conn)
	{
			 bugids =  new ArrayList<String>();
		
			 Statement stmt =  null;
			 String bug_q= "select * from selected_bugs";
			 
			 try 
			 {
				
				 stmt =  conn.createStatement();
				 stmt.execute(bug_q);
				 ResultSet rs = stmt.getResultSet();
				 while(rs.next())
				 {
					 String bugid =  rs.getString("bugid");
					 bugids.add(bugid);
				 }		
				 
			  } catch (SQLException e) 
			  {	e.printStackTrace(); }
			  
			/* for(int i=0;i<bugids.size();i++)
			 {
				 System.out.println("bugids="+bugids.get(i));
			 }*/
			  
			  
	return bugids;		  
			 
	}//process




private void insert_date_to_table(String issueKey, Timestamp date_reported)
{
  Statement stmt=null;
  String q= "insert into bugid_date values('"+issueKey+"','"+date_reported+"')";
  try 
  {
	  	stmt=conn.createStatement();
	  	stmt.executeUpdate(q);
      
  } catch (SQLException e) 
  { 	e.printStackTrace(); }
  
	 
}


private java.sql.Timestamp getDateCreated(String content) 
{
	int fromIndex=content.indexOf("create-date");
	int start= content.indexOf(">", fromIndex);
	int end=content.indexOf("<", fromIndex);
	
	String date_str= content.substring(start+1, end);
	
	java.sql.Timestamp date_reported = getDate(date_str);
		
	return date_reported;
}


//insert to db function	
//@Uses 1: insert to bug_svn_info
//@Uses 2: insert to svn_info	
private void insert_to_db(String Key, String Type, String Summary, String Description, String Components,
			String Status, String Resolution, int hasAttachment, String Reporter, String AssigneeName,
			String Svn_ids, String Svn_logmess, String Svn_files, String Svn_auth)
{

	Statement stmt=null;
	//System.out.println("REsolution="+Resolution+"  len="+Resolution.length());
	String insert="insert into bug_svn_info values('"+ Key+"','"+Type+"', '"+Summary+"' , '"+Description+"' ,'"+
	           Components+"' ,'"+Status+"' , '"+Resolution+"' ,"+hasAttachment+" ,'"+Reporter+"','"+AssigneeName+"','"+
			 Svn_ids+"' , '"+Svn_logmess+"' , '"+ Svn_files+"' , '"+Svn_auth+"');";
	System.out.println("Query="+insert);
	
	try {
			stmt=conn.createStatement();
			stmt.executeUpdate(insert);
			stmt.close();
		} 
	
	catch(DataTruncation d)
	{
		d.printStackTrace();
		try {
			BufferedWriter bw =  new BufferedWriter(new FileWriter("/home/sangeeta/Desktop/jbos_data_trcu.txt",true));
		    bw.write("Key="+Key);
		    bw.write("\n--------------------------------\n");
		    if(bw!=null)
		    {
		    	bw.close();
		    }
		
		
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
	}
	
	catch (SQLException e) 
		{
			e.printStackTrace();
		
		}
	
	
}//insert to db



//To insert values to svn_info table
/*`svnid` varchar(30)  NOT NULL,
  `file_no` int  NOT NULL,
  `file_changed` text  NOT NULL,
  `logmess` text  NOT NULL,
  `svn_auth` varchar(50)  NOT NULL,
  `date` TIMESTAMP  NOT NULL,
  `repository` varchar(50)  NOT NULL,
 * */
public void insert_svn_to_db(ArrayList<svn_object> svn_commit_info)
{
	Statement stmt=null;
	
	try {
		
		  stmt=conn.createStatement();
	} catch (SQLException e1)
	{
	
		e1.printStackTrace();
	}
	
	//if()
	//{
		for(int i=0;i<svn_commit_info.size();i++)
			{
				String svnid=svn_commit_info.get(i).revision;
				String logmess=svn_commit_info.get(i).message;
				String svn_auth=svn_commit_info.get(i).user;
				java.sql.Timestamp commit_date=svn_commit_info.get(i).date;
				String repository=svn_commit_info.get(i).repository;
				ArrayList<file_info> file_changed=svn_commit_info.get(i).file_changed; 
		   
				for(int j=0;j<file_changed.size();j++)
					{
						String file_name=file_changed.get(j).file_name;
						String file_stat = file_changed.get(j).file_stat;
						
						int file_no=j+1;
						String insert="insert into svn_info values ('"+svnid+"',"+file_no+", '"+file_name+"' ,'" +
						file_stat+"','"+logmess+"' , '"+svn_auth+"' , '"+commit_date+"' , '"+repository+"')";
			   
							try
							{
									
								stmt.executeUpdate(insert);
			   		
							} 	
							catch(DataTruncation d)
							{d.printStackTrace();
							try {
								BufferedWriter bw =  new BufferedWriter(new FileWriter("/home/sangeeta/Desktop/jbos_data_trcu.txt",true));
							    bw.write("svnid="+svnid);
							    bw.write("\n--------------------------------\n");
							    if(bw!=null)
							    {
							    	bw.close();
							    }
							
							
							} catch (IOException e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}
							}
							catch (SQLException e) 
							{e.printStackTrace();}
					} //for
				}//for	


			try
				{
					stmt.close();
				} 
			     catch (SQLException e) 
			     { e.printStackTrace();
			     
			     
			     }
    
    
}//fun
	
//Return the information from svn_commit_info() object, the information is returned in "," separated values
public String format_svn_commit_info(ArrayList<svn_object> svn_commit_info, int case_no)
{
	//Return "0" if there are no svn commits with a bug report.
	if(svn_commit_info.size()==0)
	{
		return "0";
	}
	
	else
	{
		switch(case_no)
			{
				case 1: //id
						String svn_id="";
						for(int i=0;i<svn_commit_info.size();i++)
						{
							svn_id+=svn_commit_info.get(i).revision+",";
							//System.out.println("Svn_id="+svn_id);
							
						}
						//System.out.println("SVn id****"+svn_id);
						return svn_id;
						
				case 2: //log_mess
						String svn_log_mess="";
						for(int i=0;i<svn_commit_info.size();i++)
						{
							svn_log_mess+=svn_commit_info.get(i).message+"\n";
							//System.out.println(i+"mess="+svn_log_mess);
						}
				 
						return svn_log_mess;
			     
				
				case 3://svn_files
						String svn_files="";
						for(int i=0;i<svn_commit_info.size();i++)
						{
							ArrayList<file_info> files_info =svn_commit_info.get(i).file_changed;
							for(int j=0;j<files_info.size();j++)
							{
								String file_stat =  files_info.get(j).file_stat;
								svn_files+=file_stat+" "+files_info.get(j).file_name+"\n";
							}
						}
						
						return svn_files;
					
				
						
				case 4://svn_auth
					  	String svn_auth="";
					    for(int i=0;i<svn_commit_info.size();i++)
					    {
					    	svn_auth+=svn_commit_info.get(i).user+",";
					    }
					    
					  	return svn_auth;
					   
			}//switch
	}//else

	
	return "1(for debugging)";
}
	
//return the svn information attcahed with bug report
public ArrayList<svn_object> getSvnInfo(String content)
{
   ArrayList<svn_object> svn_commit_info=new ArrayList<svn_object>();
	
   int Sindex=content.indexOf("<strong>Subversion Commits</strong>");
   int Eindex=content.indexOf("<div id=\"addcomment\" class=\"module\">");
   
   //System.out.println("Content==============="+content+"\n\n==================");
   //int Eindex=content.indexOf("<div id=\"add");
   
  // System.out.println("Sindex="+Sindex+" Eindex="+Eindex);
   String svn_info_str=content.substring(Sindex, Eindex);
   //System.out.println("Svn Info="+svn_info_str);
   
   
   String regex="<b>Repository</b>";
   Pattern p=Pattern.compile(regex);
   Matcher m=p.matcher(svn_info_str);
   
   while(m.find())
   {
	   int s1=m.start();//get the index of rep
	   int s2=svn_info_str.indexOf("<tr>",s1);
	   
	   //rep
	   int s3=svn_info_str.indexOf(">",s2+5);
	   int e3=svn_info_str.indexOf("<",s3);
	   String repository=svn_info_str.substring(s3+1,e3);
	  
	  System.out.println("Rep="+repository);
	  	     
	  //revision
	   int s4=svn_info_str.indexOf(">#", e3);//>#123 bcus revision id is mentioned like this.
	   int e4=-1;
	   String revision="";
	   if(s4!=-1)
	   {
		   e4=svn_info_str.indexOf("<",s4);
		   revision=svn_info_str.substring(s4+2, e4);
		   
	   }
	 
	   else
	   {
		   s4=svn_info_str.indexOf("3\">",e3);
		   e4=svn_info_str.indexOf("<",s4);
		   revision=svn_info_str.substring(s4+3, e4);
		   for(int z=890;z<896;z++)
		   {
			   System.out.println(svn_info_str.charAt(z));
		   }
		   System.out.println("Else e3="+e3 +"  e4="+ e4);
		   System.out.println("Else rev"+ revision);
		   
	   }
	   
	   //System.out.println("e4="+e4);
	   System.out.println("rev="+revision);
	   
	   
	   //Date	   
	   int s5=svn_info_str.indexOf(">", e4+10);//date
	   int e5=svn_info_str.indexOf("<",s5);
	   String date_str=svn_info_str.substring(s5+1, e5);
	   
	  // System.out.println("Date="+date_str);
	   java.sql.Timestamp date=getDate2(date_str);
	   
	   //user
	   int s6=svn_info_str.indexOf(">", e5+5);
	   int e6=svn_info_str.indexOf("<",s6);
	   String user=svn_info_str.substring(s6+1,e6);
	   
	  // System.out.println("user="+user);
	   
	   //mess
	   
	   int s7=svn_info_str.indexOf("<td", e6);
	   int e7=svn_info_str.indexOf("</td>",s7);
	   String mess_str=svn_info_str.substring(s7,e7);
	   //System.out.println("mess_str="+mess_str);
	   
	   String message="";
	   int prev=mess_str.indexOf(">", 0);
	   int next=mess_str.indexOf("<", prev);
	   while(next!=-1)
	   {
		  
		   message+=mess_str.subSequence(prev+1, next);
		   prev=mess_str.indexOf(">",next);
		   next=mess_str.indexOf("<", prev);
		}
	   
	   message+=mess_str.substring(prev+1);
	   //System.out.println("Log---Message="+message);//Actual message string
	   
	   
	   //changed_path
	   
	   ArrayList<file_info> file_changed= new ArrayList<file_info>();
	   int s8=svn_info_str.indexOf("<b>Files Changed</b",e7 );//bcus we need to start from the point where log message ends.
	   int e8=svn_info_str.indexOf("</table>",e7);
	   String file_change_str=svn_info_str.substring(s8, e8);
	   System.out.println("File change str="+file_change_str);//@Change file
	   
	   String mod_regex=">MODIFY</b>";
	   Pattern mod_p=Pattern.compile(mod_regex);
	   Matcher mod_m=mod_p.matcher(file_change_str);
	   while(mod_m.find())
	   {
		   int temp_index=file_change_str.indexOf("<a href", mod_m.start());
		   if(temp_index!=-1)
		   {
		   
			   System.out.println("Temp index="+temp_index);
		   
			   int s9=file_change_str.indexOf(">", temp_index);
			   int e9=file_change_str.indexOf("</a>", s9);
		   
			   String change_path=file_change_str.substring(s9+1,e9);
			  
		       
			   //
			   int begin=change_path.indexOf(">");
			   int end=change_path.indexOf("<");
			   if(end==-1)
				   end=change_path.length();
			   change_path=change_path.substring(begin+1, end);
			   /////
			   
			   change_path=change_path.trim();
			   System.out.println("Change path="+change_path);//@@Change path
			   file_changed.add(new file_info(change_path,"M"));// added change path
		   }
		   
		   else
		   {
			   //System.out.println("I am in ADD");
			   temp_index=file_change_str.indexOf("</font>", mod_m.start());
			   //System.out.println("Temp index="+temp_index);
			   
			   int s11=file_change_str.indexOf("/", temp_index+7);
			   int e11=file_change_str.indexOf("<br>", s11);
			   //System.out.println("s10="+s10+" e10="+e10);
			   
			   String change_path=file_change_str.substring(s11+1,e11).trim();
			  
			   
			   //
			   int begin=change_path.indexOf(">");
			   int end=change_path.indexOf("<");
			   if(end==-1)
				   end=change_path.length();
			   change_path=change_path.substring(begin+1, end);
			   /////
			   change_path=change_path.trim();
			   System.out.println("ADD path1="+change_path);//@@Change path
			   file_changed.add(new file_info(change_path,"M"));// ad
		   }
		   
		   
	   }//wh mod_p.find()
	   
	   
	   //If files are added to repository.
	   String add_regex=">ADD</b>";
	   Pattern add_p=Pattern.compile(add_regex);
	   Matcher add_m=add_p.matcher(file_change_str);
	   while(add_m.find())
	   {
		   
		   //System.out.println("I am in ADD");
		   int temp_index=file_change_str.indexOf("</font>", add_m.start());
		   //System.out.println("Temp index="+temp_index);
		   
		   int s10=file_change_str.indexOf("/", temp_index+7);
		   int e10=file_change_str.indexOf("<br>", s10);
		   //System.out.println("s10="+s10+" e10="+e10);
		   
		   String change_path=file_change_str.substring(s10+1,e10).trim();
		  
		   
		   //
		   int begin=change_path.indexOf(">");
		   int end=change_path.indexOf("<");
		   if(end==-1)
			   end=change_path.length();
		   change_path=change_path.substring(begin+1, end);
		   /////
		   change_path=change_path.trim();
		   System.out.println("ADD path2="+change_path);//@@Change path
		   file_changed.add(new file_info(change_path,"A"));// added change path
	   }
	   
	     
	   //System.out.println("Message +add: "+message);
	   svn_object svn_obj= new svn_object(repository, revision,date,user,message,file_changed);
	   svn_commit_info.add(svn_obj);// add the current svn_object to arrayList.
	   
	  	   
   }//wh
   
      
   return svn_commit_info;
}//


public String getDescription(String content)
{
	String Description=""; 
	int index= content.indexOf(">Description</h3>");
	//System.out.println("index="+index);
	 
	if(index!=-1)
		{
			int s1=content.indexOf("<div", index);
			int e1=content.indexOf("</div>",s1);
			//System.out.println("s1="+s1+ " e1"+e1);
			
			String RoughDesc=content.substring(s1,e1);
			//System.out.println("Rough Des="+RoughDesc);@@Desc in <HTML> format
			
			
			//String regex="<.*>";
			//Pattern p=Pattern.compile(regex);
			//Matcher m=p.matcher(RoughDesc);
			
			int prev=0;
            int next=0;
            do
            {
            	next=RoughDesc.indexOf("<",prev);
            	
            	
            	//System.out.println("Prev="+prev+ "  next="+next);
            	if((next!=-1)&&(prev!=next)&&(prev+1<next-1))
            	{
            		Description+=RoughDesc.substring(prev+1, next);
            		Description+=" ";
            	}	
            	
            	//System.out.println("DEs="+Description);
            	prev=RoughDesc.indexOf(">", next);
            	
            }while(next!=-1);
		

			
		}	
	
	
	//Clean Description
	//Description.replaceAll("\\&", "\\*\\*\\*");
	//Description.indexOf("&");
	Description = Description.replaceAll("\"", " ");
	Description = Description.replaceAll("'", " ");
	Description = Description.replaceAll("&apos;", " ");
	Description = Description.replaceAll("&quot;", " ");
	Description = Description.replaceAll("&lt;", " ");
	Description = Description.replaceAll("&gt;", " ");
	Description = Description.replaceAll("&#\\d\\d+", "");
	Description = Description.replaceAll("/", " ");
	
	Description= Description.trim();
	
	//System.out.println("Description="+Description);
	return Description;
}
	
public ArrayList<history_object> getHistory(String content)
{
		
	   
	
		ArrayList<history_object> history_obj= new ArrayList<history_object>();
		
		//Retrieve
		String regex ="user-hover user-avatar";
		Pattern p=Pattern.compile(regex, Pattern.CASE_INSENSITIVE);
		Matcher m=p.matcher(content);
		Vector<String> result=new Vector<String>();
			
		//System.out.println("Here");
		while(m.find())
		 {
			
			String s=m.group(0);
			int index=m.start(0);
			//result.add(s);
			//System.out.println("str="+s+"index="+index);
			
			int s1=content.indexOf(">",index);
			int e1=content.indexOf("</a>", index);
			
			String history_auth=content.substring(s1+1, e1);
			//System.out.println("Auth name="+history_auth);//@Auth name
			
			int s2=content.indexOf("'date'",index);
			int e2=content.indexOf("</span>",index);
			
			String date_str=content.substring(s2+7,e2);
			//System.out.println("date str"+date_str);//@date_str
			
			java.sql.Timestamp change_date=getDate(date_str);
			
			
			int s3=content.indexOf("<table",index);
			int e3=content.indexOf("</table>",index);
			//System.out.println("s3"+s3+"e3"+e3);//@@
			
			String auth_activity_substr=content.substring(s3,e3);
			System.out.println("Substr"+auth_activity_substr);
			
			ArrayList<FieldModInfo> history_activity=new ArrayList<FieldModInfo>();			
			history_activity=getHistoryActivity(history_activity,auth_activity_substr);
			
			history_object ho= new history_object(history_auth,change_date,history_activity);
			
			
						
		 }
				
		
		
	return history_obj;	
		
}//history
	


public ArrayList<FieldModInfo>getHistoryActivity(ArrayList<FieldModInfo> history_activity, String auth_activity_substr)
{
	
	String regex2="activity-name";
	Pattern p2=Pattern.compile(regex2,Pattern.CASE_INSENSITIVE);
	Matcher m2=p2.matcher(auth_activity_substr);
	
	while(m2.find())
	{
		
		int index2=m2.start();
		//System.out.println("index2="+index2+" str="+m2.group(0));
		
		String field="";
		int sf1=auth_activity_substr.indexOf(">", index2);
		int ef1=auth_activity_substr.indexOf("</td>",index2);
		field=auth_activity_substr.substring(sf1+1,ef1).trim();
		
		//System.out.println("Field"+field);
		
		
		String original="";
		int sf2=auth_activity_substr.indexOf("activity-old-val", index2);
		int ef2=auth_activity_substr.indexOf("</td>", sf2);
		original=auth_activity_substr.substring(sf2+18, ef2).trim();
		original=clean_and_add(original);
		
		//System.out.println("original="+original);
		
			
		
		String newValue="";
		int sf3=auth_activity_substr.indexOf("activity-new-val",index2);
		int ef3=auth_activity_substr.indexOf("</td>", sf3);
		newValue=auth_activity_substr.substring(sf3+18, ef3);
		newValue=clean_and_add(newValue);
		//System.out.println("New Value="+newValue);
		
		FieldModInfo obj= new FieldModInfo(field, original,newValue);
		history_activity.add(obj);
		
	}	
	
	return history_activity;
}


//This function is used to remove extra things from the string given in in history fields like [1]
public String clean_and_add(String original)
{
  
	String modified="";
	if(original.contains("hist-value"))
	{
		int e1=original.indexOf("<");
		String temp=original.substring(0,e1).trim();
		
		
		int s2=original.indexOf(">");
		int e2=original.indexOf("</span");
		temp+=original.substring(s2+1,e2).trim();
		//System.out.println("Temp="+temp);		
		
		modified=temp;
		
	}
	else
	{
		modified=original;
	}
  
  
 return modified;

}


public java.sql.Timestamp getDate(String date_str)
{


	//System.out.println("change_date="+change_date);
	
	
	java.sql.Timestamp change_date = null;
	SimpleDateFormat sdf=new SimpleDateFormat("dd/MMM/yy hh:mm a");
	
	
	try {
		    
		    java.util.Date date=sdf.parse(date_str);
		    change_date=new java.sql.Timestamp(date.getTime());
		    System.out.println("Change  date="+change_date);
		    
		    
	 } 
	  catch (ParseException e) 
	  { e.printStackTrace(); }
	
	
	return change_date;

}

//Converting Date of the format Thu Dec 16 14:16:28 EST 2010 to mysql
//@Used for Subversion commits.
public java.sql.Timestamp getDate2(String date_str)
{
  java.sql.Timestamp date=null;
  
  String date_arr[]=date_str.split("\\s+");
  String month_name[]={"jan","feb","mar","apr","may","jun","jul","aug","sep","oct","nov","dec"};
  List l=Arrays.asList(month_name);
  Vector<String> month_vect=new Vector<String>(l);
  
  int month_index=month_vect.indexOf(date_arr[1].toLowerCase());
  
  String month="0";
  if(month_index<10)
  {
	  month=month+month_index+"";
  }
  else
  {
	  month=month_index+"";
  }
  
  String date_str_temp=date_arr[5]+"-"+month+"-"+date_arr[2]+" "+date_arr[3]+".0";
  //System.out.println("date="+date_str_temp);
  date=java.sql.Timestamp.valueOf(date_str_temp);
  return date;
  
}//get date2()

public String getContent2(String issueid)
{
	String content="";
	
	try {
		  
		  BufferedReader br= new BufferedReader(new FileReader("/home/sangeeta/jboss_data/ISPN-835.txt"));
		  String line_val= br.readLine();
		  while(line_val!=null)
		  {
			  content+=line_val;
			  line_val=br.readLine();
		  }
	
	} catch (FileNotFoundException e) 
	
	{e.printStackTrace();}
	
	catch(IOException e)
	{e.printStackTrace();}
	
	
	return content;
}

public String getContent(String issueid, String issue_path)
{
	/*
	String userPassword="sangeetalal"+":"+"iiitd123";
	sun.misc.BASE64Encoder base64Encoder = new sun.misc.BASE64Encoder();
	String encoding = base64Encoder.encode (userPassword.getBytes());
	
	
	
	System.out.println(jboss+issueid+issue_path);
    URL url;
    String content="";
 
    try {
			//url = new URL(jboss+issueid+issue_path);
    	    url= new URL("https://issues.jboss.org/browse/JBAS-7470?page=com.atlassian.jira.plugin.ext.subversion%3Asubversion-commits-tabpanel#issue-tabs");
			URLConnection conn=url.openConnection();
			conn.setRequestProperty ("Authorization", "Basic " + encoding);//added@@remove
			
			conn.setDoOutput(true);
			InputStream in=null;
			in=url.openStream();
			content = pipe(in,"utf-8");
	
    } catch (MalformedURLException e) 
	{			
		System.out.println("caught1");
		e.printStackTrace();
	}
	catch (IOException e)
	{
		System.out.println("caught2");
		e.printStackTrace();
	}
	catch(Exception e)
	{
		e.printStackTrace();
	}
*/
	
	
	String content="";
	
	String authString = "sangeetalal" + ":" + "iiitd123";
	//System.out.println("auth string: " + authString);
	byte[] authEncBytes = Base64.encodeBase64(authString.getBytes());
	String authStringEnc = new String(authEncBytes);
	//System.out.println("Base64 encoded auth string: " + authStringEnc);
	
	try {
			URL url =new URL(jboss+issueid+issue_path);
			URLConnection urlConnection = url.openConnection();
			urlConnection.setRequestProperty("Authorization", "Basic " + authStringEnc);
			InputStream is = urlConnection.getInputStream();
			InputStreamReader isr = new InputStreamReader(is);
			int numCharsRead;
			char[] charArray = new char[1024];
			StringBuffer sb = new StringBuffer();
			while ((numCharsRead = isr.read(charArray)) > 0) {
				sb.append(charArray, 0, numCharsRead);
				}
			content = sb.toString();
			
	  } 
	    catch (MalformedURLException e) 
	     { e.printStackTrace();}
	    catch(IOException e)
	    {e.printStackTrace();}
	    catch(Exception e)
	    {e.printStackTrace();}
	

	
	return content;
}
	

public String pipe(InputStream in,String charset) 
{
		StringBuffer s = new StringBuffer();
		
		
		if(charset==null||"".equals(charset))
		{
			charset="utf-8";
		}
		
		String rLine = null;
		BufferedReader bReader;
		try {
				
			
				bReader = new BufferedReader(new InputStreamReader(in,charset));
					
				while ( (rLine = bReader.readLine()) != null) 
				{
		
						String tmp_rLine = rLine;
						int str_len = tmp_rLine.length();
		
						if (str_len > 0) 
							{
								s.append(tmp_rLine);
								
							}
	
						tmp_rLine = null;
				}//while
		
		} catch (UnsupportedEncodingException e) 
		{
    		e.printStackTrace();
		}
		catch(FileNotFoundException e)
		{ 
			e.printStackTrace();
		}
		catch(IOException e)
		{
		   e.printStackTrace();	
		}
		
		try 
		{
			 in.close();
		
		 } catch (IOException e) 
		 {
		
		 	e.printStackTrace();
		 }	
		
		return s.toString();
		
}


	
public static void main(String[] args) 
{
		
	
		db_conn db = new db_conn();
		JBossIssueDownloader jid = new JBossIssueDownloader();
		//conn=db.connect("jboss", "root","123");
		conn  =  db.connect(jid.dbname, jid.userid, jid.passwd);
		jid.process();
		
		db.close(conn);

}//main

}